package cn.scau.server.controller;


import cn.scau.server.pojo.Notice;
import cn.scau.server.service.NoticeService;
import cn.scau.server.vo.ResultVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 代码不跑我先跑
 * @since 2025-03-30
 */
@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @PostMapping("/add")
    @ApiOperation(value = "新增公告", notes = "创建并发布公告或保存到草稿箱")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "公告标题", required = true, dataType = "String"),
            @ApiImplicitParam(name = "content", value = "公告内容", required = true, dataType = "String"),
            @ApiImplicitParam(name = "status", value = "公告状态(0未发布 1已发布)", required = true, dataType = "int"),
            @ApiImplicitParam(name = "deptId", value = "部门编号", required = true, dataType = "int")
    })
    public ResultVO<Notice> publishNotice(
            @RequestParam String title,
            @RequestParam String content,
            @RequestParam Integer status,
            @RequestParam Integer deptId) {
        return noticeService.publishNotice(title, content, status, deptId);
    }

    @GetMapping("/published")
    @ApiOperation(value = "查询已发布公告（分页）")
    public ResultVO<Page<Notice>> getPublishedNotices(
            @RequestParam(required = false) Integer id,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate,
            @RequestParam(required = false) Integer deptId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return noticeService.getPublishedNotices(id, title, startDate, endDate, deptId, pageNum, pageSize);
    }

    @GetMapping("/drafts")
    @ApiOperation(value = "查询草稿公告（分页）")
    public ResultVO<Page<Notice>> getDraftNotices(
            @RequestParam(required = false) Integer id,
            @RequestParam(required = false) String title,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) Integer deptId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return noticeService.getDraftNotices(id, title, status, deptId, pageNum, pageSize);
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑公告", notes = "编辑未发布或已撤销的公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "公告编号", required = true, dataType = "int"),
            @ApiImplicitParam(name = "deptId", value = "部门编号", required = true, dataType = "int"),
            @ApiImplicitParam(name = "title", value = "公告标题", required = false, dataType = "String"),
            @ApiImplicitParam(name = "content", value = "公告内容", required = true, dataType = "String")
    })
    public ResultVO<Notice> editNotice(
            @RequestParam Integer id,
            @RequestParam Integer deptId,
            @RequestParam String title,
            @RequestParam String content) {
        return noticeService.editNotice(id, deptId, title, content);
    }

    @PutMapping("/changeStatus")
    @ApiOperation(value = "改变公告状态", notes = "发布或撤销公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "公告编号", required = true, dataType = "int"),
            @ApiImplicitParam(name = "status", value = "目标状态(1发布 2撤销)", required = true, dataType = "int")
    })
    public ResultVO<Notice> changeNoticeStatus(
            @RequestParam Integer id,
            @RequestParam Integer status) {
        return noticeService.changeNoticeStatus(id, status);
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除公告", notes = "删除未发布或已撤销的公告")
    @ApiImplicitParam(name = "id", value = "公告编号", required = true, dataType = "int")
    public ResultVO deleteNotice(@RequestParam Integer id) {
        return noticeService.deleteNotice(id);
    }

    @GetMapping("/view")
    @ApiOperation(value = "查看公告", notes = "根据公告编号获取公告信息")
    @ApiImplicitParam(name = "id", value = "公告编号", required = true, dataType = "int")
    public ResultVO<Notice> viewNoticeContent(@RequestParam Integer id) {
        return noticeService.viewNoticeContent(id);
    }
}
